﻿using PRO.DbProvider;
using PRO.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using PRO.Common;
using PRO.ORM;

namespace PRO.Data.SqlServer
{
    public partial class DataProvider : IDataProvider
    {
        public int CreateOrder(OrderInfo order, DataTable products)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" begin transaction declare @errorSum int ");
            sql.Append(" declare @newid int ");
            sql.Append(" insert into Pro_Order (userid,orderid,title,des,note,totalprice,cardnumber,consignee,address,zip,moble,phone,status,buytime) values(@userid,@orderid,@title,@des,@note,@totalprice,@cardnumber,@consignee,@address,@zip,@moble,@phone,@status,@buytime); ");
            sql.Append(" set @newid=(select @@IDENTITY) ");
            sql.Append(" set @errorSum=@errorSum+@@error ");
            for (int i = 0; i < products.Rows.Count; i++)
            {
                sql.Append("insert into Pro_Order_Items (oid,producttitle,price,counts) values(@newid,'" + products.Rows[i]["title"].ToString().SqlFilter().RemoveHTML() + "'," + products.Rows[i]["price"].ToString() + "," + products.Rows[i]["count"].ToString() + ")");
                sql.Append(" set @errorSum=@errorSum+@@error ");
            }
            sql.Append(" select @newid ");
            sql.Append(@" if @errorSum>0
                            begin
                                rollback transaction
                            end
                           else
                            begin
                                commit transaction
                            end");
            DbParameter[] parameters = {
					DbHelper.MakeInParam("@userid", (DbType)SqlDbType.Int,4,order.UserID),
					DbHelper.MakeInParam("@orderid", (DbType)SqlDbType.NVarChar,20,order.OrderID),
                    DbHelper.MakeInParam("@title", (DbType)SqlDbType.NVarChar,200,order.Title.RemoveHTML()),
					DbHelper.MakeInParam("@des", (DbType)SqlDbType.NVarChar,500,order.Des.RemoveHTML()),
					DbHelper.MakeInParam("@note", (DbType)SqlDbType.NVarChar,500,order.Note.RemoveHTML()),
					DbHelper.MakeInParam("@totalprice", (DbType)SqlDbType.Decimal,6,order.TotalPrice),
                    DbHelper.MakeInParam("@cardnumber", (DbType)SqlDbType.NVarChar,12,order.CardNumber),
                    DbHelper.MakeInParam("@consignee", (DbType)SqlDbType.NVarChar,20,order.Consignee.RemoveHTML()),
                    DbHelper.MakeInParam("@address", (DbType)SqlDbType.NVarChar,200,order.Address.RemoveHTML()),
                    DbHelper.MakeInParam("@zip", (DbType)SqlDbType.NVarChar,6,order.Zip),
                    DbHelper.MakeInParam("@moble", (DbType)SqlDbType.NVarChar,11,order.Moble),
                    DbHelper.MakeInParam("@phone", (DbType)SqlDbType.NVarChar,30,order.Phone.RemoveHTML()),
                    DbHelper.MakeInParam("@status", (DbType)SqlDbType.Int,4,order.Status),
					DbHelper.MakeInParam("@buytime", (DbType)SqlDbType.DateTime,8,order.BuyTime)
                };
            return DbHelper.ExecuteScalar(CommandType.Text, sql.ToString(), parameters).ToInt(0);
        }
        public DataTable GetOrderInfo(int id, int userid)
        {
            SqlHelper helper = new SqlSelect("Pro_Order");
            helper.AddFeild("id", "userid", "orderid", "title", "des", "note", "totalprice", "cardnumber", "consignee", "address", "zip", "moble", "phone", "status", "buytime");
            helper.AddWhereAnd("id", id, SqlDbType.Int, 4, "=");
            helper.AddWhereAnd("userid", userid, SqlDbType.Int, 4, "=");
            return (DataTable)helper.Run();
        }
        public DataTable GetOrderItems(int orderid)
        {
            SqlHelper helper = new SqlSelect("Pro_Order_Items");
            helper.AddFeild("id", "oid", "producttitle", "price", "counts");
            helper.AddWhereAnd("oid", orderid, SqlDbType.Int, 4, "=");
            return (DataTable)helper.Run();
        }
       
    }
}
